package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.utils.UserContext;
import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelCommentService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.utils.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommendService travelCommendService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @PostMapping
    @ResponseBody
    public Object save(Travel travel){
        JSONResult result = new JSONResult();
        try {
            travel.setAuthor(UserContext.getCurrentUser());
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }


    @PutMapping
    @ResponseBody
    public Object update(Travel travel){
        JSONResult result = new JSONResult();
        try {
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;
    }

    @GetMapping
    @ResponseBody
    public Object getTravel(Long id){

        return travelService.getTravelById(id);
    }

    @GetMapping("/list")
    @ResponseBody
    public Object list(@ModelAttribute("qo") TravelQueryObject qo){
        //必须是公开
        qo.setState(Travel.STATE_RELEASE);
        //必须是发布状态
        qo.setIsPublic(true);
        //设置排序
        qo.setOrderBy("t.releaseTime desc");

        PageInfo pageInfo = travelService.query(qo);

        return pageInfo;
    }

    @GetMapping("/commends")
    @ResponseBody
    public Object getCommends(@ModelAttribute("qo") TravelCommendQueryObject qo){
        return travelCommendService.queryApp(qo);
    }
    @GetMapping("/{travelId}/likeCount")
    @ResponseBody
    public Object getLikeCount(@PathVariable Long travelId,Long userId){
        Integer count = travelService.queryLikeCount(travelId);
        JSONResult result = new JSONResult();
        result.setObj(count);
        if (userId!=null){

            if (travelService.queryIfexist(travelId,userId)==1){
                return result;
            }else {
                result.setSuccess(false);
                return result;
            }
        }else {
            result.setSuccess(false);
            return result;
        }
    }
    @PostMapping("/like/save")
    @ResponseBody
    public Object saveLike(Long travelId,Long userId){
        JSONResult result = new JSONResult();
        try {
            travelService.saveLike(userId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @DeleteMapping("/like/delete")
    @ResponseBody
    public Object delete( Long travelId,Long userId){
        JSONResult result = new JSONResult();
        try {
            travelService.deleteLike(userId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    //查询所有攻略的标题---------------------------------------
    @GetMapping("/search")
    @ResponseBody
    public Object getStrategies(){
        return travelService.searchTitle();
    }

    //根据标题搜索攻略跳转--------------------------------------
    @GetMapping("/titles")
    @ResponseBody
    public Object getStrategies(String title){
        return travelService.selectStrategyByTitle(title);
    }
    //获取指定游记的评论
    @GetMapping("/{travelId}/comments")
    @ResponseBody
    public Object getTravelCommentsByTravelId(@ModelAttribute("qo") TravelCommentQueryObject qo){
        return travelCommentService.query(qo);
    }

    //浏览量
    @GetMapping("/{id}/browse")
    @ResponseBody
    public Object getBrowse(@PathVariable Long id){
        JSONResult result = new JSONResult();
        try {
            travelService.updateBrowse(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;

    }

    //保存游记一级评论
    @PostMapping("/comments")
    @ResponseBody
    public Object save(TravelComment travelComment){
        JSONResult result = new JSONResult();
        try {
            travelCommentService.save(travelComment);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
///travels/"+params.id+"/likeCount